home *** CD-ROM | disk | FTP | other *** search
/ Revista CD Expert 8 / Revista CD Expert nº 08 CD1.iso / Utilitarios / Programacao / Pacific C for DOS / EXAMPLES / U2D.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-08  |  1.8 KB  |  90 lines

  1. #include    <stdio.h>
  2. #include    <sys.h>
  3. #include    <stdlib.h>
  4.  
  5. /*
  6.  *    Unix To DOS text file conversion utility. 
  7.  *
  8.  *    Converts the Unix style end-of-line sequence 0AH
  9.  *    to MS-DOS style end-of-line sequence 0DH 0AH
  10.  */
  11.  
  12. #define    TMPNAME    "$U2D$TMP.$$$"
  13.  
  14. /*
  15.  *    int    fixfile(char * filename)
  16.  *
  17.  *    "Fix" the text file specified by "filename".
  18.  *    Returns 0 on error, non zero otherwise.
  19.  */
  20.  
  21. int
  22. fixfile(char * filename)
  23. {
  24.     FILE *        infile;
  25.     FILE *        outfile;
  26.     char        ch;
  27.     unsigned long    in, out;
  28.  
  29.     printf("u2d: %s: ", filename);
  30.     fflush(stdout);
  31.     infile = fopen(filename, "r");    /* try to open input file */
  32.     if (!infile) {
  33.         printf("not found\n");
  34.         return 0;
  35.     }
  36.     outfile = fopen(TMPNAME, "w");    /* try to open scratch file */
  37.     if (!outfile) {
  38.         fclose(infile);
  39.         printf("unable to open temp file %s\n", TMPNAME);
  40.         return 1;
  41.     }
  42.     in = out = 0;
  43.     while ((ch = fgetc(infile)) != EOF) {    /* while not end of file */
  44.         if (fputc(ch, outfile) == EOF) {
  45.             fclose(infile);
  46.             fclose(outfile);
  47.             printf("error writing to temp file %s\n", TMPNAME);
  48.             return 1;
  49.         }
  50.         ++in;
  51.         ++out;
  52.         if (ch == '\n')
  53.             ++out;
  54.     }
  55.     fclose(infile);
  56.     fclose(outfile);
  57.     if (remove(filename) != 0) {
  58.         printf("unable to delete %s\n", filename);
  59.         return 1;
  60.     }
  61.     if (rename(TMPNAME, filename) != 0) {
  62.         printf("unable to rename %s to %s\n", TMPNAME, filename);
  63.         return 1;
  64.     }
  65.     printf("converted: %lu bytes read, %lu bytes written\n", in, out);
  66.     return 0;
  67.  
  68. }
  69.  
  70. main(int argc, char ** argv)
  71. {
  72.     int    i;
  73.  
  74.     if (argc == 1) {
  75.         argv = _getargs(0, "u2d");
  76.         argc = _argc_;
  77.     }
  78.     if (argc == 1) {
  79.         fprintf(stderr, "u2d: usage: u2d file1 file2 ...\n");
  80.         fprintf(stderr, "u2d: wildcards, e.g. *.c are expanded\n");
  81.         exit(1);
  82.     }
  83.     for (i = 1; i != argc; i++) {
  84.         if (fixfile(argv[i])) {
  85.             fprintf(stderr, "u2d: aborted!\n");
  86.             exit(1);
  87.         }
  88.     }
  89. }
  90.